﻿using CarPCBATest.Procedure;
using Common;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using static OfficeOpenXml.ExcelErrorValue;

namespace CarPCBATest.ViewModel
{
    internal class DebugViewModel : NotifyBase
    {
        private bool loaded = false;
        private bool _ioPower1;

        /// <summary>
        /// 产品供电1
        /// </summary>
        public bool IOPower1
        {
            get { return _ioPower1; }
            set
            {
                _ioPower1 = value;
                Global.IOModule1.SetDOValue((int)DODefine.Power1, value ? 1 : 0);
                //IOModule.SetDOValue(DODefine.Power, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _ioPower2;

        /// <summary>
        /// 产品供电2
        /// </summary>
        public bool IOPower2
        {
            get { return _ioPower2; }
            set
            {
                _ioPower2 = value;
                Global.IOModule1.SetDOValue((int)DODefine.Power2, value ? 1 : 0);
                //IOModule.SetDOValue(DODefine.Power, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _inProgram;

        /// <summary>
        /// 绿灯
        /// </summary>
        public bool InProgram
        {
            get { return _inProgram; }
            set
            {
                _inProgram = value;
                //IOModule.SetDOValue(DODefine.OK, value ? 1 : 0);
                Global.IOModule1.SetDOValue((int)DODefine.InProgram, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _ioOKLed;

        /// <summary>
        /// 绿灯
        /// </summary>
        public bool IOOkLed
        {
            get { return _ioOKLed; }
            set
            {
                _ioOKLed = value;
                //IOModule.SetDOValue(DODefine.OK, value ? 1 : 0);
                Global.IOModule1.SetDOValue((int)DODefine.GreenLight, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _ioNgLed;

        /// <summary>
        /// 报警灯
        /// </summary>
        public bool IONgLed
        {
            get { return _ioNgLed; }
            set
            {
                _ioNgLed = value;
                Global.IOModule1.SetDOValue((int)DODefine.RedLight, value ? 1 : 0);
                //IOModule.SetDOValue(DODefine.NG, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _burn2;

        public bool Burn2
        {
            get { return _burn2; }
            set
            {
                _burn2 = value;
                Global.IOModule1.SetDOValue((int)DODefine.BurnIO, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _canfdCom;

        public bool CanfdCom
        {
            get { return _canfdCom; }
            set
            {
                _canfdCom = value;
                Global.IOModule1.SetDOValue((int)DODefine.CanfdCom, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioAir;

        /// <summary>
        /// 气缸下压
        /// </summary>
        public bool IOAir
        {
            get { return _ioAir; }
            set
            {
                _ioAir = value;
                //IOModule.SetDOValue(DODefine.AirReset, value ? 1 : 0);
                Global.IOModule1.SetDOValue((int)DODefine.AirReset, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _ioLineSwitch;

        /// <summary>
        /// 高电平烧录低电平测试
        /// </summary>
        public bool IOLineSwitch
        {
            get { return _ioLineSwitch; }
            set
            {
                _ioLineSwitch = value;
                //IOModule.SetDOValue(DODefine.LineSwitch, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioSafe;

        /// <summary>
        /// 安全光幕
        /// </summary>
        public bool IOSafe
        {
            get { return _ioSafe; }
            set
            {
                _ioSafe = value;
                this.DoNotify();
            }
        }

        private bool _ioStartButton;

        /// <summary>
        /// 启动按钮
        /// </summary>
        public bool IOStartButton
        {
            get { return _ioStartButton; }
            set
            {
                _ioStartButton = value;
                this.DoNotify();
            }
        }

        private bool _ioEmgStop;

        /// <summary>
        /// 急停按钮
        /// </summary>
        public bool IOEmgStop
        {
            get { return _ioEmgStop; }
            set
            {
                _ioEmgStop = value;
                this.DoNotify();
            }
        }

        private bool _ioResetButtton;

        /// <summary>
        /// 复位按钮
        /// </summary>
        public bool IOResetButton
        {
            get { return _ioResetButtton; }
            set
            {
                _ioResetButtton = value;
                this.DoNotify();
            }
        }

        private bool _ioAirWorkSenser;

        /// <summary>
        /// 气缸到位
        /// </summary>
        public bool IOAirWorkSenser
        {
            get { return _ioAirWorkSenser; }
            set
            {
                _ioAirWorkSenser = value;
                this.DoNotify();
            }
        }

        private string _current;

        /// <summary>
        /// 电流表电流
        /// </summary>
        public string Current
        {
            get { return _current; }
            set
            {
                _current = value;
                this.DoNotify();
            }
        }

        private double _power;

        /// <summary>
        /// 电压表设置电压
        /// </summary>
        public double Power
        {
            get { return _power; }
            set
            {
                _power = value;
                this.DoNotify();
            }
        }

        private int _selectedChannel;

        /// <summary>
        /// 电压表通道
        /// </summary>
        public int SelectedChannel
        {
            get { return _selectedChannel; }
            set
            {
                _selectedChannel = value;
                this.DoNotify();
            }
        }

        private double _current2;

        /// <summary>
        /// 电压表电流
        /// </summary>
        public double Current2
        {
            get { return _current2; }
            set
            {
                _current2 = value;
                this.DoNotify();
            }
        }

        public CommandBase GetCurrentCommand { get; set; }
        public CommandBase SetPowerCommand { get; set; }
        public CommandBase SetCurrentCommand { get; set; }
        public CommandBase UnloadedCommand { get; set; }
        public CommandBase LoadedCommand { get; set; }
        public CommandBase SetRemoteCommand { get; set; }
        public CommandBase OnChannelCommand { get; set; }
        public CommandBase OffChannelCommand { get; set; }
        public CommandBase IT6302Command { get; set; }

        public DebugViewModel()
        {
            try
            {
                SelectedChannel = 1;
                GetCurrentCommand = new CommandBase(GetCurrent);
                SetPowerCommand = new CommandBase(SetPower);
                SetCurrentCommand = new CommandBase(SetCurrent);
                UnloadedCommand = new CommandBase(Unloaded);
                LoadedCommand = new CommandBase(Loaded);
                SetRemoteCommand = new CommandBase(SetRemote);
                OnChannelCommand = new CommandBase(OnChannel);
                OffChannelCommand = new CommandBase(OffChannel);
                IT6302Command = new CommandBase(IT6302Exec);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void IT6302Exec(object obj)
        {
            try
            {
                int cmdIndex = int.Parse(obj.ToString());
                switch (cmdIndex)
                {
                    case 1:
                        var vol = Global.IT6302.MeasVoltage(SelectedChannel);
                        Power = vol;
                        break;

                    case 2:
                        var cur = Global.IT6302.MeasCurrent(SelectedChannel);
                        Current2 = cur;
                        break;

                    default:
                        break;
                }
                Global.IT6302.SetVoltage(SelectedChannel, Power);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OffChannel(object obj)
        {
            try
            {
                Global.IT6302.SetOutputStatus(SelectedChannel, false);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OnChannel(object obj)
        {
            try
            {
                Global.IT6302.SetOutputStatus(SelectedChannel, true);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetRemote(object obj)
        {
            try
            {
                Global.IT6302.SetRemote();
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void Loaded(object obj)
        {
            try
            {
                //DM3058E.Connect();
                //IT6302A.Connect();

                //IOModule.Connect();
                //Thread.Sleep(100);
                loaded = true;
                Task.Run(() =>
                {
                    while (loaded)
                    {
                        IOStartButton = Global.IOModule1.GetDIValue((int)DIDefine.Start);
                        IOSafe = Global.IOModule1.GetDIValue((int)DIDefine.SafeLight);
                        IOAirWorkSenser = Global.IOModule1.GetDIValue((int)DIDefine.AirWork);
                        IOEmgStop = Global.IOModule1.GetDIValue((int)DIDefine.EmgStop);

                        InProgram = Global.IOModule1.GetDOValue((int)DODefine.InProgram);
                        IOOkLed = Global.IOModule1.GetDOValue((int)DODefine.GreenLight);
                        IOPower1 = Global.IOModule1.GetDOValue((int)DODefine.Power1);
                        IOPower2 = Global.IOModule1.GetDOValue((int)DODefine.Power2);
                        IOAir = Global.IOModule1.GetDOValue((int)DODefine.AirReset);
                        IONgLed = Global.IOModule1.GetDOValue((int)DODefine.RedLight);
                        IOLineSwitch = Global.IOModule1.GetDOValue((int)DODefine.InProgram);
                        Burn2 = Global.IOModule1.GetDOValue((int)DODefine.BurnIO);
                        CanfdCom = Global.IOModule1.GetDOValue((int)DODefine.CanfdCom);

                        Thread.Sleep(500);
                    }
                });
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void Unloaded(object obj)
        {
            loaded = false;
        }

        private void SetCurrent(object obj)
        {
            try
            {
                Global.IT6302.SetCurrent(SelectedChannel, Current2);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetPower(object obj)
        {
            try
            {
                Global.IT6302.SetVoltage(SelectedChannel, Power);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void GetCurrent(object obj)
        {
            try
            {
                Current = Global.DM3058E.GetValue().ToString();
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }
    }
}